<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Log;

define('SECRETKEY','dP5jyWrCJOTohKC768TtGEqc8FQTLBgg');
class ApiController extends Controller
{

//{
//"uid":"用户id",
//"sex":"用户性别",
//"nickname":"用户昵称",
//"phone":"用户手机号",
//"club":"总会名称",
//"branch":"分会名称",
//"company":"公司名称",
//"timestamp":"1584151623",
//"sign":"uid、phone、timestamp 排序后的MD5值"
//}
//
//    {
//        "uid":"用户id",
//	"code":"aaaaa",
//	"timestamp":"1584151623",
//	"sign":"uid、code、timestamp 排序后的MD5值"
//	"datas":[
//		{
//            "temperature" : "36.5",
//			"timestamp":"2020-3-14 10:24:15",
//		},
//		{
//            "temperature" : "36.5",
//			"timestamp":"2020-3-14 10:24:15",
//		}
//	]
//}



    public function get_data(Request $request)
    {
        //dd($request->getContent());
        $get_data = json_decode($request->getContent(),true);
        $post_sign = $get_data['sign'];

        $sign_data = [
            'uid' => $get_data['uid'],
            'code' => $get_data['code'],
            'timestamp' => $get_data['timestamp']
        ];

        //验证签名
        $sign = $this -> md5_sign($sign_data);
        
        // 签名匹配
        if ($sign == $post_sign)
        {
            $post_data['error_code'] = '00000';
            $post_data['error'] = '请求成功';
        }else
        {
            $post_data['error_code'] = '20001';
            $post_data['error'] = '参数错误';
        }

        return json_encode($post_data);
    }

    public function get_userinfo(Request $request)
    {
        //dd($request->getContent());
        $get_data = json_decode($request->getContent(),true);
        $post_sign = $get_data['sign'];

        $sign_data = [
            'uid' => $get_data['uid'],
            'code' => $get_data['code'],
            'timestamp' => $get_data['timestamp']
        ];

        //验证签名
        $sign = $this -> md5_sign($sign_data);

        // 签名匹配
        if ($sign == $post_sign)
        {
            $post_data['error_code'] = '00000';
            $post_data['error'] = '请求成功';
        }else
        {
            $post_data['error_code'] = '20001';
            $post_data['error'] = '参数错误';
        }

        return json_encode($post_data);
    }


    //签名加密
    private function md5_sign($data)
    {
        ksort($data);
        $str='';
        foreach($data as $k => $v){
            $str .= $k.'='.$v.'&';
        }
        // 引入配置文件的秘钥
        $secretkey = SECRETKEY;
        $res = $str.'key='.$secretkey;
        $sign = md5($res);
        return $sign;
    }

    public function test()
    {
        $data['uid'] = 1;
        $data['code'] = 1233111;
        $data['timestamp'] = time();
        $data['datas'] = [
            [
                'temperature' => 36.6,
                'timestamp' => date('Y-m-d H:i:s')
            ],
            [
                'temperature' => 36.6,
                'timestamp' => date('Y-m-d H:i:s')
            ],
        ];
        $sign_data = [
            'uid' => $data['uid'],
            'code' => $data['code'],
            'timestamp' => $data['timestamp']
        ];

        $data['sign'] = $this->md5_sign($sign_data);
        $url = 'http://apicats.com/api/get_data';
        //$url = 'http://mytiwen.com/api/get_data';
        $res = sendCurl_json($url,json_encode($data));
        echo $res;
    }


    public function test_userinfo()
    {
        $data['uid'] = 1;
        $data['sex'] = 0; //0男1女
        $data['nickname'] = 'zhangsan';
        $data['phone'] = 18788888888;
        $data['code'] = 1233111;
        $data['club'] = '总会名';
        $data['branch'] = '分会名';
        $data['company'] = '公司名';
        $data['timestamp'] = time();

        $sign_data = [
            'uid' => $data['uid'],
            'code' => $data['code'],
            'timestamp' => $data['timestamp']
        ];

        $data['sign'] = $this->md5_sign($sign_data);
        $url = 'http://apicats.com/api/get_userinfo';
        //$url = 'http://mytiwen.com/api/get_userinfo';
        $res = sendCurl_json($url,json_encode($data));
        echo $res;
    }


}
